/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.sim;

import com.jme3.math.Vector3f;
import com.jme3.math.Vector4f;
import static technotheist.geo.map.MapUtil.PANELS;
import static technotheist.geo.map.MapUtil.SphereVolume;
import technotheist.geo.map.scalar.FloatMap;
import technotheist.geo.map.vector.Float4Map;
import technotheist.geo.map.vector.UnitMap;

/**
 *
 * @author Mitchel Strachan
 */
public class GeoLayer {

    protected final UnitMap unit;
    protected final GeoLayerBound lowerBound;
    protected final GeoLayerBound upperBound;
    protected final Float4Map position;

    
    
    public GeoLayer(UnitMap unit, int size) {
        this.unit = unit;
        this.lowerBound = new GeoLayerBound(size);
        this.upperBound = new GeoLayerBound(size);
        this.position = new Float4Map(size);
    }
    
    public GeoLayer(int size, GeoLayer lower) {
        this.unit = lower.unit;
        this.lowerBound = lower.upperBound;
        this.upperBound = new GeoLayerBound(size);
        this.position = new Float4Map(size);
    }
    
    protected void updateVolume() {
        Vector3f pos = new Vector3f();
        Vector4f vol = new Vector4f();
        float el = 1.0f / position.elementCount;
        for (int p = 0; p < PANELS; p++) {
            for (int u = 0; u < position.sideLength; u++) {
                for (int v = 0; v < position.sideLength; v++) {
                    float nu = u * position.uvNormalize;
                    float nv = v * position.uvNormalize;
                    //TODO : x,y,z
                    unit.get(p, nu, nv, pos);
                    vol.set(pos.x, pos.y, pos.z, 
                            SphereVolume(upperBound.elevation(p, nu, nv)) 
                            - SphereVolume(lowerBound.elevation(p, nu, nv))
                            * el);
                    position.set(p, u, v, vol);
                }
            }
        }
    }
    
    protected float uvNormalize() 
    {
        return position.uvNormalize;
    }
    
    public int north() 
    {
        return position.elementCount-1;
    }
    
    public int south() 
    {
        return position.elementCount-2;
    }
    
    
}
